package com.fenqing.utils;

import cn.hutool.json.JSONUtil;
import com.fenqing.bean.Node;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fenqing
 */
public class Node1ToStringEncoder implements Encoder<Node, String> {
    @Override
    public String encoder(Node input) {
        List<List<Integer>> lists = new ArrayList<>();
        encoder(input, lists);
        return JSONUtil.toJsonStr(lists);
    }

    public void encoder(Node input, List<List<Integer>> lists){
        int val = input.val;
        while (lists.size() < val) {
            lists.add(null);
        }
        List<Integer> integers = lists.get(val - 1);
        if (integers != null) {
            return;
        }
        integers = new ArrayList<>();
        lists.set(val - 1, integers);
        for (Node neighbor : input.neighbors) {
            integers.add(neighbor.val);
            encoder(neighbor, lists);
        }

    }
}
